package main

import (
	"fmt"
	"strconv"
	"strings"
)

//TenToTwo 比如十进制转二进制 短除法
func TenToTwo(n int) string {
	res := ""
	for ; n > 0; n /= 2 {
		lsb := n % 2
		res = strconv.Itoa(lsb) + res
	}
	return res
}

//TwoToTen 二进制转十进制
func TwoToTen(s string) int {
	var res int
	for i := 0; i < len(s); i++ {
		a := s[len(s)-i-1]
		res += int(a-48) * power(2, i) ////string的1对应的byte是49 所以要减48 以此类推
	}
	return res
}

//power 幂运算 x^n x的n次方
func power(x int, n int) int {
	ans := 1
	for n != 0 {
		if n%2 == 1 {
			ans *= x
		}
		x *= x
		n /= 2
	}
	return ans
}

//TenToSixteen 十进制转十六进制
func TenToSixteen(ten int) string {
	m := 0
	hex := make([]int, 0)
	for {
		m = ten % 16
		ten = ten / 16
		if ten == 0 {
			hex = append(hex, m)
			break
		}
		hex = append(hex, m)
	}
	var hexStr []string
	for i := len(hex) - 1; i >= 0; i-- {
		if hex[i] >= 10 {
			hexStr = append(hexStr, fmt.Sprintf("%c", 'A'+hex[i]-10))
		} else {
			hexStr = append(hexStr, fmt.Sprintf("%d", hex[i]))
		}
	}
	return strings.Join(hexStr, "")
}

//调用自带的接口
func yuan() {
	var v int64 = 12              //默认10进制
	s2 := strconv.FormatInt(v, 2) //10 转2进制
	fmt.Printf("%v\n", s2)

	s8 := strconv.FormatInt(v, 8)
	fmt.Printf("%v\n", s8)

	s10 := strconv.FormatInt(v, 10)
	fmt.Printf("%v\n", s10)

	s16 := strconv.FormatInt(v, 16) //10 to 16
	fmt.Printf("%v\n", s16)

	var sv = "11"
	fmt.Println(strconv.ParseInt(sv, 16, 32)) // 16 to 10
	fmt.Println(strconv.ParseInt(sv, 10, 32)) // 10 to 10
	fmt.Println(strconv.ParseInt(sv, 8, 32))  // 8 to 10
	fmt.Println(strconv.ParseInt(sv, 2, 32))  // 2 to 10
}

func main() {

}
